<?php

App::uses('CrudController', 'Controller');

class TrainingsController extends CrudController
{
    public $_model = 'Training';

    public function index($lineId = null) {
        $this->request->allowMethod('get');
        $appBosch = new stdClass();
        $appBosch->line = $lineId;
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Mis Certificaciones'));
        $this->set('description', __('En entrenamiento'));
    }
    
    public function evaluations() {
        $this->request->allowMethod('get');
        $userId = $this->Auth->user('id');
        $this->loadModel('Evaluation');
        $appBosch = new stdClass();
        $appBosch->data = $this->Evaluation->getSuccessByUser($userId);
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Mis cursos'));
    }

    public function getDetails($certId)
    {
        $this->request->allowMethod('get');
        $userId = $this->Auth->user('id');
        $this->layout = null;
        $this->loadModel('CertificationDetail');
        $this->loadModel('Certification');
        $records['training'] = $this->CertificationDetail->getByCertification($certId, $userId);
        $records['left'] = $this->Certification->getLeftForCertification($certId);
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function admin() {
        $this->request->allowMethod('get');
        $appBosch = new stdClass();
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Certificaciones'));
        $this->set('description', __('Extensión'));
    }

    public function getNextToEnd() {
        $m = $this->_model;
        $userId = $this->Auth->user('id');
        $records = $this->$m->getEnabledNextToEndByUser($userId);
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function updateDateEnd() {
        $this->request->allowMethod('get');
        $records = "error";
        $permissions = $this->getPermissionsCertifications();
        if ($permissions->haveModifyEnd()) {
            $this->loadModel('LogCertification');
            $cert = $this->request->query['models'];
            $cert = json_decode($cert);
            $m = $this->_model;
            $certUp = array();
            $certUp['id'] = $cert->value;
            $certUp['end'] = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $cert->end)->format('Y-m-d H:i:s');
            if ($this->$m->save($certUp)) {
                $records = $cert;
                $this->$m->clear();
                $this->$m->id = $cert->value;
                $reg = $this->$m->read();
                $this->LogCertification->addUpdateLog($this->Auth->user('id'), $reg[$m], 'Modificación fin de proceso');
            }
        }
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function diploma($idEvaluation) {
        $this->loadModel('Diploma');
        $res = $this->Diploma->findByEvaluationId($idEvaluation);
        $nRecord = $res['Diploma'];
        $nRecord['data'] = json_decode($nRecord['data']);
        $this->set('record', $nRecord);
        $appBosch = new stdClass();
        $this->set('appBosch', $appBosch);
    }

    protected function getRecords($param = null)
    {
        $m = $this->_model;
        $user = $this->Auth->user();
        $records = $this->$m->getEnabled($user['id'], $param);
        return $records;
    }

    protected function c($model) {
        return array(
            'text' => $model->text,
            'name' => $model->text,
        );
    }

    protected function u($model) {
        return array(
            'text' => $model->text,
            'name' => $model->text,
        );
    }

    protected function id($model) {
        return $model->value;
    }

    public function evaluationsByNomina() {
        $this->request->allowMethod('get');
        $appBosch = new stdClass();
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Diplomas'));
    }

    public function getTrainings($nomina) {
        $this->loadModel('Evaluation');
        $this->loadModel('User');
        $userId = $this->User->findByNominaAndStatusAndCategoryId(trim($nomina), User::STATUS_ENABLED, Category::ASOCIADO);
        if (isset($userId['User'])) {
            $userId = $userId['User']['id'];
            $records = $this->Evaluation->getSuccessByUser($userId);
        } else {
            $records = false;
        }
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }
}
